﻿namespace EFS.Common.Serialization
{
	/// Used for the statuses message list
	public class SerializableCollection<T> : EFS.Common.Serialization.ISerializable, System.Collections.IEnumerable
		where T : EFS.Common.Serialization.ISerializable
	{
		/// Public constructor (used especially for deserialization)
		public SerializableCollection()
		{
			// ensure is never null
			Items = new T[0];
		}

		/// Explicit constructor
		public SerializableCollection(T[] items)
		{
			// ensure is never null
			Items = new T[0];

			// test array
			if (null != items)
			{
				Items = items;
			}
		}

		/// Explicit constructor
		public SerializableCollection(System.Collections.Generic.ICollection<T> collection)
		{
			// init 
			Items = new T[collection.Count];

			// copy
			collection.CopyTo(Items, 0);
		}

		/// Property Items: Get
		public T[] Items;

		/// Serialize
		public void Serialize(EFS.Common.Serialization.XmlWriter xmlWriter)
		{
			// create start
			xmlWriter.Write<T>("Items", "Item", Items);
		}

		/// Deserialize
		public void Deserialize(EFS.Common.Serialization.XmlReader xmlReader)
		{
			// create start
			xmlReader.Read<T>("Items", "Item", ref Items);
		}

		/// <summary>
		/// Get enumerator
		/// </summary>
		/// <returns></returns>
		public System.Collections.IEnumerator GetEnumerator()
		{
			return Items.GetEnumerator();
		}
	}

}